package com.jic.data.controller;

import cn.hutool.core.date.DateUtil;
import com.jic.data.model.vo.response.ProductSaleDetailResponse;
import com.jic.data.model.vo.response.HistogramChartResponse;
import com.jic.data.model.vo.response.SaleDetailResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

import com.jic.common.base.vo.RestResult;
import lombok.extern.slf4j.Slf4j;
import com.jic.data.model.vo.request.SaleCategoryDetailRequest;
import com.jic.data.model.vo.response.SaleCategoryDetailResponse;
import com.jic.data.model.entity.SaleCategoryDetail;
import com.jic.data.service.SaleCategoryDetailService;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-05-09 17:25:18
 */
@RestController
@Slf4j
@RequestMapping("/saleCategoryDetail")
@Api(tags = " [商品销售-品类-90-销售品类明细]控制类-PC")
public class SaleCategoryDetailController {
    @Autowired
    private SaleCategoryDetailService service;


    // TODO 修改完成
    @PostMapping("/list/sale/categoryDatail")
    @ApiOperation(value = "销售明细查询")
    @ResponseBody
    public RestResult<List<SaleDetailResponse<ProductSaleDetailResponse<List<SaleCategoryDetailResponse>>>>> listSaleCategoryDetailAllRecord(@RequestBody SaleCategoryDetailRequest request) throws Throwable {
        SaleCategoryDetail saleCategoryDetail = new SaleCategoryDetail();
        BeanUtils.copyProperties(request, saleCategoryDetail);
        List<SaleDetailResponse<ProductSaleDetailResponse<List<SaleCategoryDetailResponse>>>> responseList = service.listCategorySaleDetail(saleCategoryDetail);
        return RestResult.success(responseList);
    }


    // TODO 修改完成
    @PostMapping("/list/top/categy")
    @ApiOperation(value = "热销品类TOP10")
    @ResponseBody
    public RestResult<HistogramChartResponse> listSaleTopCategory(@RequestBody SaleCategoryDetailRequest request) throws Throwable {
        SaleCategoryDetail saleCategoryDetail = new SaleCategoryDetail();
        BeanUtils.copyProperties(request, saleCategoryDetail);
        HistogramChartResponse response = service.listTopCategory(saleCategoryDetail);

        return RestResult.success(response);
    }

    /**
     * SALE_CATEGORY_DETAIL 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [商品销售-品类-90-销售品类明细] ")
    @ResponseBody
    public RestResult<List<SaleCategoryDetailResponse>> listAll(@RequestBody SaleCategoryDetailRequest request) throws Throwable {
        SaleCategoryDetail saleCategoryDetail = new SaleCategoryDetail();
        BeanUtils.copyProperties(request, saleCategoryDetail);
        List<SaleCategoryDetail> list = service.listAllRecord(saleCategoryDetail);
        List<SaleCategoryDetailResponse> responseList = list.stream().map(temp -> {
            SaleCategoryDetailResponse bo = new SaleCategoryDetailResponse();
            BeanUtils.copyProperties(temp, bo);
            bo.setFormatSaleDate(DateUtil.format(bo.getSaleDate(), "yyyy年MM月"));
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }


}

